package com.runda.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.bbjob.mapper.ExamInfoMapper;
import com.bbjob.mapper.ExamineeInfoMapper;
import com.bbjob.mapper.QuestionBankMapper;
import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.model.ExamInfo;
import com.bbjob.model.ExamineeInfo;
import com.bbjob.model.QuestionBank;
import com.bbjob.model.SysOrganization;
import com.bbjob.util.service.impl.BaseService;
import com.runda.service.IClientService;
import com.runda.util.DataEntity;

@Service
public class ClientServiceImpl extends BaseService<ExamineeInfo> implements IClientService{
	
	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	@Resource
	private ExamineeInfoMapper examineeInfoMapper;
	@Resource
	private ExamInfoMapper examInfoMapper;
	@Resource
	private QuestionBankMapper questionBankMapper;
	
	@Override
	public DataEntity login(ExamineeInfo examineeInfo) {
		if (examineeInfo.getUniquecode() != null && !"".equals(examineeInfo.getUniquecode())) {
			Example organExample = new Example(SysOrganization.class);
			organExample.createCriteria().andEqualTo("uniqueCode", examineeInfo.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
			
			if (organs.size() > 0) {
				examineeInfo.setOrganId(organs.get(0).getId());
			} else {
				return new DataEntity("8106", false, "未查询到学校信息");
			}
			
			Example examineeInfoExample = new Example(ExamineeInfo.class);
			examineeInfoExample.createCriteria().andEqualTo("organId", examineeInfo.getOrganId()).andEqualTo("sNo", examineeInfo.getsNo()).andEqualTo("eNo", examineeInfo.geteNo()).andEqualTo("field1", examineeInfo.getField1());
			List<ExamineeInfo> loginInfos = examineeInfoMapper.selectByExample(examineeInfoExample);
			
			if (loginInfos.size() > 0) {
				DataEntity result = new DataEntity("200", true, "登录成功");
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				for (ExamineeInfo loginInfo : loginInfos) {
					Integer eId = loginInfo.geteId();
					Map<String, Object> examInfoMap = new HashMap<String, Object>();
					
					ExamInfo examInfo = examInfoMapper.selectByPrimaryKey(eId);
					/*String qIds = "";
					if (examInfo != null) {
						qIds = examInfo.getqId() == null ? "" : examInfo.getqId();
					}*/
	/*				Long sEndtime = examInfo.geteEndtime().getTime();
					Long nTime = new Date().getTime();
					if (nTime > sEndtime) {
						return new DataEntity("8106", true, "登录失败，该考试已结束");
					}*/

					String qId = loginInfo.getField2();// Tool.getRandomBankId(qIds);
					
					QuestionBank bank = questionBankMapper.selectByPrimaryKey(qId);
					
					String relaticePath = "";
					if (bank != null) {
						relaticePath = "/" + eId + "/" + qId;
					}
					
					examInfoMap.put("eId", eId);
					examInfoMap.put("qId", qId);
					examInfoMap.put("sNo", examineeInfo.getsNo());
					examInfoMap.put("eNo", examineeInfo.geteNo());
					examInfoMap.put("field1", examineeInfo.getField1());
					examInfoMap.put("paperUrl", relaticePath);
					examInfoMap.put("deliveryExamInfo", examInfo);
					examInfoMap.put("serverTime", new Date());
					
					list.add(examInfoMap);
				}
				// result.setData(list.get(0));
				result.setData(list);		
				return result;
			} else {
				return new DataEntity("8106", false, "登录失败，请仔细核对登录信息");
			}
		} else {
			
			return new DataEntity("8106", false, "学校唯一编码为空");
		}
	}
	
	
}
